Imports System.Diagnostics.Process
Imports System.Threading
Public Class F_Grouping
    Dim WithEvents SBO_Application As SAPbouiCOM.Application
    Dim Ocompany As SAPbobsCOM.Company
    Sub New(ByVal ocompany1 As SAPbobsCOM.Company, ByVal sbo_application1 As SAPbouiCOM.Application)
        SBO_Application = sbo_application1
        Ocompany = ocompany1
    End Sub
    Public Sub Grouping_bind(ByVal oform As SAPbouiCOM.Form)
        Try
            CFL_BP_PetMaster_Attendance(oform, SBO_Application)
            ' CFL_BP_Employee_Mobile(oform, SBO_Application)
            oCombo = oform.Items.Item("21").Specific
            ComboLoad_Program(oform, oCombo)
            oEdit = oform.Items.Item("6").Specific
            oEdit.String = Format(Now.Date, "dd/MM/yy")
            oEdit = oform.Items.Item("23").Specific
            oEdit.String = Format(Now.Date, "dd/MM/yy")
            oEdit = oform.Items.Item("8").Specific
            oEdit.String = "Open"
            oEdit = oform.Items.Item("36").Specific
            oEdit.ChooseFromListUID = "CFLPMATT"
            oEdit.ChooseFromListAlias = "U_AI_PetCode"
            'oEdit = oform.Items.Item("1000003").Specific
            'oEdit.ChooseFromListUID = "CFLEMPM"
            'oEdit.ChooseFromListAlias = "U_AI_MobileUser"
            oform.DataBrowser.BrowseBy = "4"
            'oMatrix = oform.Items.Item("1000001").Specific
            'oColumns = oMatrix.Columns
            DocNum()
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try

    End Sub
    Public Sub MatrixLoad()
        oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        oEdit = oForm.Items.Item("23").Specific
        Dim dt1 As String = oEdit.Value
        oRecordSet.DoQuery("SELECT T0.[U_AI_PetCode], T0.[U_AI_PetName], T0.[U_AI_Comments] FROM [dbo].[@AI_ATTENDANCE]  T0 WHERE T0.[U_AI_Date] ='" & dt1 & "' and  T0.[U_AI_TickHealth] ='Yes'")
        oRecordSet.MoveFirst()
        oCombo = oForm.Items.Item("21").Specific
        Dim progcode As String = oCombo.Selected.Value
        oEdit = oForm.Items.Item("23").Specific
        'Dim dt1 As String = oEdit.String
        'Dim dt As Date = ParamDate(dt1)
        'Dim dt2 As String = oEdit.Value
        ' Dim dt3 As Date = ParamDate(dt2)
        Dim samp As String = oEdit.Value
        Dim i As Integer
        Dim k As Integer
        For i = 1 To oMatrix.RowCount
            oMatrix.DeleteRow(oMatrix.RowCount)
        Next
        Dim str As String = "SELECT T1.[U_PetCode], T1.[U_YN] FROM [dbo].[@AI_GROUPING]  T0 , [dbo].[@AI_GROUPING_LINE]  T1 WHERE T1.[DocEntry] = T0.[DocNum] and T0.[U_AI_ProgCode] ='" & progcode & "' and  T0.[U_AI_Date] ='" & samp & "' and  T1.[U_YN] ='Y'"
        oRecordSet1.DoQuery(str)
        For i = 1 To oRecordSet.RecordCount
            Dim check As Boolean = False
            oRecordSet1.MoveFirst()
            For k = 1 To oRecordSet1.RecordCount
                If oRecordSet1.Fields.Item(0).Value = oRecordSet.Fields.Item(0).Value Then
                    check = True
                End If
                oRecordSet1.MoveNext()
            Next
            If check = False Then
                oMatrix.AddRow()
                oEdit = oMatrix.Columns.Item("V_3").Cells.Item(oMatrix.RowCount).Specific
                oEdit.String = oRecordSet.Fields.Item(0).Value
                oEdit = oMatrix.Columns.Item("V_2").Cells.Item(oMatrix.RowCount).Specific
                oEdit.String = oRecordSet.Fields.Item(1).Value
                'oEdit = oMatrix.Columns.Item("V_1").Cells.Item(oMatrix.RowCount).Specific
                'oEdit.String = oRecordSet.Fields.Item(2).Value
                oCheck = oMatrix.Columns.Item("V_0").Cells.Item(oMatrix.RowCount).Specific
                oCheck.Checked = True
            End If
            oRecordSet.MoveNext()
        Next
    End Sub
    Function ParamDate(ByVal dt As String) As Date
        Dim d, m, y As String
        Dim Z As Date
        d = dt.Substring(0, 2)
        m = dt.Substring(3, 2)
        y = dt.Substring(6, 2)
        'Z = CDate("May 25, 1986")
        Z = CDate(d & "/" & m & "/" & y)
        'Return System.DateTime.Parse(dt, format1, Globalization.DateTimeStyles.None)
        Return Z
    End Function
    Function GopiDate(ByVal dt As String) As String
        Dim d, m, y As String
        Dim Z As Date
        d = dt.Substring(0, 2)
        m = dt.Substring(3, 2)
        y = dt.Substring(6, 2)
        'Z = CDate("May 25, 1986")
        Z = d & "/" & m & "/" & y
        'Return System.DateTime.Parse(dt, format1, Globalization.DateTimeStyles.None)
        Return Z
    End Function
    Private Sub ComboLoad_Program(ByRef Oform As SAPbouiCOM.Form, ByRef oCombo As SAPbouiCOM.ComboBox)
        Try
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.Code, T0.Name FROM [dbo].[@AI_PROGRAM]  T0 where t0.U_AI_ForGrouping='Y'")
            Dim it As Integer = 1
            For it = oCombo.ValidValues.Count - 1 To 0 Step -1
                oCombo.ValidValues.Remove(it, SAPbouiCOM.BoSearchKey.psk_Index)
            Next
            Do While Not oRecordSet.EoF
                oCombo.ValidValues.Add(oRecordSet.Fields.Item(0).Value, oRecordSet.Fields.Item(1).Value)
                oRecordSet.MoveNext()
            Loop
            oRecordSet = Nothing
            GC.Collect()
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Public Sub DocNum()
        Try
            Dim DocNum As Integer = 0
            Dim DocNumLen As Integer
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT (cast(max(T0.[DocNum] + 1) as int)) as DocNum  FROM [dbo].[@AI_GROUPING]  T0")
            oEdit = oForm.Items.Item("11").Specific
            Dim oEdit1 As SAPbouiCOM.EditText
            oEdit1 = oForm.Items.Item("13").Specific
            DocNum = oRecordSet.Fields.Item(0).Value
            DocNumLen = DocNum.ToString.Length
            If DocNum = 0 Then
                oEdit.String = "G" & "00000"
                oEdit1.String = "G" & "00000"
            ElseIf DocNumLen = 1 And DocNum <> 0 Then
                oEdit.String = "G" & "0000" & DocNum
                oEdit1.String = "G" & "0000" & DocNum
            ElseIf DocNumLen = 2 Then
                oEdit.String = "G" & "000" & DocNum
                oEdit1.String = "G" & "000" & DocNum
            ElseIf DocNumLen = 3 Then
                oEdit.String = "G" & "00" & DocNum
                oEdit1.String = "G" & "00" & DocNum
            ElseIf DocNumLen = 4 Then
                oEdit.String = "G" & "0" & DocNum
                oEdit1.String = "G" & "00" & DocNum
            ElseIf DocNumLen >= 5 Then
                oEdit.String = "G" & DocNum
                oEdit1.String = "G" & DocNum
            End If
            oRecordSet = Nothing
            GC.Collect()
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Private Sub SBO_Application_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.ItemEvent
        'Choose From List
        Try
            If pVal.FormUID = "AI_GROUPING" Then
                oForm = SBO_Application.Forms.Item("AI_GROUPING")
                If pVal.EventType = SAPbouiCOM.BoEventTypes.et_CHOOSE_FROM_LIST Then
                    Dim oCFLEvento As SAPbouiCOM.IChooseFromListEvent
                    oCFLEvento = pVal
                    Dim sCFL_ID As String
                    sCFL_ID = oCFLEvento.ChooseFromListUID
                    oForm = SBO_Application.Forms.Item(FormUID)
                    Dim oCFL As SAPbouiCOM.ChooseFromList
                    oCFL = oForm.ChooseFromLists.Item(sCFL_ID)
                    Try
                        If oCFLEvento.BeforeAction = False Then
                            Dim oDataTable As SAPbouiCOM.DataTable
                            oDataTable = oCFLEvento.SelectedObjects
                            If pVal.ItemUID = "36" Then
                                oEdit = oForm.Items.Item("34").Specific
                                oEdit.String = oDataTable.GetValue("U_AI_PetName", 0)
                                Try
                                    oEdit = oForm.Items.Item("36").Specific
                                    oEdit.String = oDataTable.GetValue("U_AI_PetCode", 0)
                                Catch ex As Exception
                                End Try
                            End If
                            If pVal.ItemUID = "1000003" Then
                                'oEdit = oForm.Items.Item("26").Specific
                                'oEdit.String = oDataTable.GetValue("U_U_PetName", 0)
                                Try
                                    oEdit = oForm.Items.Item("25").Specific
                                    oEdit.String = oDataTable.GetValue("firstName", 0)
                                    oEdit = oForm.Items.Item("1000003").Specific
                                    oEdit.String = oDataTable.GetValue("U_AI_MobileUser", 0)
                                Catch ex As Exception
                                End Try

                            End If
                        End If
                    Catch ex As Exception
                        '' SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                    End Try
                End If
            End If
            '--------------------
        Catch ex As Exception
        End Try
        '======================================================

        Try
            '-------------------End Choose From List-----------
            If pVal.FormUID = "AI_GROUPING" And pVal.InnerEvent = False Then
                oForm = SBO_Application.Forms.Item("AI_GROUPING")
                If pVal.Before_Action = False Then
                    Select Case pVal.EventType
                        Case SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED
                            If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then
                                If pVal.ItemUID = "1" Then
                                    oEdit = oForm.Items.Item("6").Specific
                                    oEdit.String = Format(Now.Date, "dd/MM/yy")
                                    oEdit = oForm.Items.Item("23").Specific
                                    oEdit.String = Format(Now.Date, "dd/MM/yy")
                                    oEdit = oForm.Items.Item("8").Specific
                                    oEdit.String = "Open"
                                    DocNum()

                                End If
                            End If

                        Case SAPbouiCOM.BoEventTypes.et_COMBO_SELECT
                            If pVal.ItemUID = "21" Then
                                oCombo = oForm.Items.Item("21").Specific
                                oEdit = oForm.Items.Item("19").Specific
                                oEdit.String = oCombo.Selected.Description
                                ' MatrixLoad()
                            End If

                    End Select
                ElseIf pVal.Before_Action = True Then
                    Select Case pVal.EventType
                        Case SAPbouiCOM.BoEventTypes.et_VALIDATE
                            If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE Or pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then
                                If pVal.ItemUID = "GR1000003" Then
                                    oEdit = oForm.Items.Item("GR1000003").Specific
                                    If oEdit.String <> "" Then
                                        Dim Uname As String = MobileUserName(oEdit.String, Ocompany)
                                        oEdit = oForm.Items.Item("25").Specific
                                        oEdit.String = Uname
                                    End If
                                End If

                            End If
                        Case SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED
                            If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE Or pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then


                                If pVal.ItemUID = "1" Then
                                    oEdit = oForm.Items.Item("11").Specific
                                    If oEdit.String = "" Then
                                        SBO_Application.StatusBar.SetText("Select Group Code", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                        BubbleEvent = False
                                        Exit Sub
                                    End If
                                    oEdit = oForm.Items.Item("36").Specific
                                    If oEdit.String = "" Then
                                        SBO_Application.StatusBar.SetText("Select Pet Code", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                        BubbleEvent = False
                                        Exit Sub
                                    End If
                                    'oEdit = oForm.Items.Item("15").Specific
                                    'If oEdit.String = "" Then
                                    '    SBO_Application.StatusBar.SetText("Select Pet Code", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                    '    BubbleEvent = False
                                    '    Exit Sub
                                    'End If
                                    oEdit = oForm.Items.Item("19").Specific
                                    If oEdit.String = "" Then
                                        SBO_Application.StatusBar.SetText("Select Program/Activities Name", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                        BubbleEvent = False
                                        Exit Sub
                                    End If
                                    oEdit = oForm.Items.Item("23").Specific
                                    If oEdit.String = "" Then
                                        SBO_Application.StatusBar.SetText("Select Group Date", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                        BubbleEvent = False
                                        Exit Sub
                                    End If
                                    oEdit = oForm.Items.Item("6").Specific
                                    If oEdit.String = "" Then
                                        SBO_Application.StatusBar.SetText("Select Document Date", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                        BubbleEvent = False
                                        Exit Sub
                                    End If
                                    oEdit = oForm.Items.Item("GR1000003").Specific
                                    If oEdit.String = "" Then
                                        SBO_Application.StatusBar.SetText("Select Handler/Trainier Code", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                        BubbleEvent = False
                                        Exit Sub
                                    End If
                                    Try
                                        oCombo = oForm.Items.Item("21").Specific
                                        If oCombo.Selected.Value = "" Then
                                            SBO_Application.StatusBar.SetText("Select Program/Activities Code", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If

                                    Catch ex As Exception
                                        SBO_Application.StatusBar.SetText("Select Program/Activities Code", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                        BubbleEvent = False
                                        Exit Sub
                                    End Try
                                    'If oMatrix.RowCount = 0 Then
                                    '    SBO_Application.StatusBar.SetText("No Dogs For Grouping", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                    '    BubbleEvent = False
                                    '    Exit Sub
                                    'End If
                                    ' POST_GROUPING()
                                End If
                            End If
                    End Select
                End If
            End If
        Catch ex As Exception
            '  SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try



    End Sub
    Private Sub POST_GROUPING()
        Try
            Dim i As Integer
            For i = 1 To oMatrix.RowCount
                oCheck = oMatrix.Columns.Item("V_0").Cells.Item(i).Specific
                If oCheck.Checked = True Then
                    Dim oGeneralService As SAPbobsCOM.GeneralService
                    Dim oGeneralData As SAPbobsCOM.GeneralData
                    Dim oSons As SAPbobsCOM.GeneralDataCollection
                    Dim oSon As SAPbobsCOM.GeneralData
                    Dim sCmp As SAPbobsCOM.CompanyService
                    sCmp = Ocompany.GetCompanyService
                    oGeneralService = sCmp.GetGeneralService("AI_GROUPING1")
                    oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
                    oEdit = oForm.Items.Item("11").Specific
                    oGeneralData.SetProperty("U_Code", oEdit.String)
                    oEdit = oForm.Items.Item("11").Specific
                    oGeneralData.SetProperty("U_Name", oEdit.String)
                    oEdit = oMatrix.Columns.Item("V_3").Cells.Item(i).Specific
                    oGeneralData.SetProperty("U_AI_PetCode", oEdit.String)
                    oEdit = oMatrix.Columns.Item("V_2").Cells.Item(i).Specific
                    oGeneralData.SetProperty("U_AI_PetName", oEdit.String)
                    oCombo = oForm.Items.Item("21").Specific
                    oGeneralData.SetProperty("U_AI_ProgCode", oCombo.Selected.Value)
                    oEdit = oForm.Items.Item("19").Specific
                    oGeneralData.SetProperty("U_AI_ProgName", oEdit.String)
                    oEdit = oForm.Items.Item("23").Specific
                    Dim dt As Date = MyDate(oEdit.String)
                    oGeneralData.SetProperty("U_AI_Date", dt.Date)
                    oEdit = oForm.Items.Item("1000003").Specific
                    oGeneralData.SetProperty("U_AI_HandlerCode", oEdit.String)
                    oEdit = oForm.Items.Item("25").Specific
                    oGeneralData.SetProperty("U_AI_Handler", oEdit.String)
                    oGeneralService.Add(oGeneralData)
                End If
            Next


        Catch ex As Exception

        End Try
    End Sub
    Function MyDate(ByVal dt As String) As Date
        Dim d, m, y As String
        Dim Z As Date
        d = dt.Substring(0, 2)
        m = dt.Substring(3, 2)
        y = dt.Substring(6, 2)
        Z = CDate(m & "/" & d & "/" & y)
        Return Z
    End Function
End Class
